﻿using DataAccess;
using QuanLyKhachSan.Validation;
using System;
using System.Linq;
using NLog;
using System.Windows.Forms;

namespace QuanLyKhachSan
{
    public partial class FormQuocTich : Form
    {
        QuocTichManager controller = new QuocTichManager();
        protected static Logger _logger = LogManager.GetCurrentClassLogger();
        public FormQuocTich()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtMaQT.Text = "";
            txtTenQT.Text = "";
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            QuocTich quoctich = new QuocTich();
            quoctich.MaQT = txtMaQT.Text;
            quoctich.TenQT = txtTenQT.Text;

            ValidateResult vResult = controller.Validate(quoctich);
            if (!vResult.Message.Any())
            {
                ActionResult aResult= controller.Them(quoctich);
                if (!aResult.Exceptions.Any())
                {
                    MessageBox.Show("Thêm thành công quốc tịch: "+quoctich.MaQT+" - "+quoctich.TenQT);
                    _logger.Info("Thêm thành công quốc tịch: " + quoctich.MaQT + " - " + quoctich.TenQT);
                    this.Close();
                }
                else
                {
                    string s = "";
                    foreach (var ex in aResult.Exceptions)
                    {
                        s += ex.Message + "\n";
                    }
                    MessageBox.Show(s);
                    _logger.Error(s);
                }
            }
            else
            {
                string s = "";
                foreach (var chuoi in vResult.Message)
                {
                    s += chuoi + "\n";
                }
                MessageBox.Show(s);
                _logger.Error(s);
            }
        }
    }
}
